package GUI;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mapa.Coordenada;
import mapa.Domicilio;
import odpgl.Implementacion;

import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
import org.openstreetmap.gui.jmapviewer.MapRectangleImpl;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;

import clientes.Cliente;
import clientes.Historial;

import java.awt.Frame;
import java.awt.Point;
import java.awt.Dimension;
import java.util.Locale;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class ventanaMapeoRecorrido extends JFrame {

	JFrame dialogo;
	private JMapViewer mapa;
	public Implementacion pedidosGeoLoc = new Implementacion();
	public Set<Cliente> clientes;

	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
						ventanaMapeoRecorrido window = new ventanaMapeoRecorrido();
					window.dialogo.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ventanaMapeoRecorrido()
	{
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize()
	{
		dialogo = new JFrame();
		dialogo.setBounds(new Rectangle(0, 0, 0, 0));
		dialogo.setAlwaysOnTop(true);
		dialogo.setResizable(false);
		dialogo.setPreferredSize(new Dimension(100, 200));
		dialogo.setLocale(new Locale("es", "AR"));
		dialogo.setLocationByPlatform(true);
		dialogo.setName("muestramapa");
		dialogo.setVisible(true);
		dialogo.setBounds(150, 0,1024,700);
		dialogo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);		
		JPanel panelSuperior = new JPanel();
		dialogo.getContentPane().add(panelSuperior, BorderLayout.NORTH);
		
		JPanel panelInferior = new JPanel();
		dialogo.getContentPane().add(panelInferior, BorderLayout.SOUTH);
		
		final JSlider slider = new JSlider();
		slider.setValue(10);
		slider.setMaximum(19);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0)
			{
				int valor = slider.getValue();
				mapa.setZoom(valor);
			}
		});
		panelInferior.add(slider);
		
		JButton btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialogo.dispose();
			}
		});
		panelInferior.add(btnVolver);
		
		JPanel panelIzquierdo = new JPanel();
		dialogo.getContentPane().add(panelIzquierdo, BorderLayout.WEST);
		
		JPanel panelDerecho = new JPanel();
		dialogo.getContentPane().add(panelDerecho, BorderLayout.EAST);
		
		mapa = new JMapViewer();
		mapa.setOpaque(false);
		mapa.setPreferredSize(new Dimension(800, 600));
		mapa.setMapMarkerVisible(true);
		mapa.setLocation(new Point(0, 0));
		mapa.setMinimumSize(new Dimension(0, 0));
		mapa.setMaximumSize(new Dimension(2147483647, 2147483647));
		mapa.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0)
			{
				Coordinate c = mapa.getPosition();
				dialogo.setTitle(c.getLat() + ", " + c.getLon());
			}
		});
		mapa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				slider.setValue( mapa.getZoom() );
			}
		});
		mapa.addMouseWheelListener(new MouseWheelListener() {
			public void mouseWheelMoved(MouseWheelEvent arg0)
			{
				slider.setValue( mapa.getZoom() );
			}
		});
		mapa.setZoomContolsVisible(false);
		mapa.setDisplayPositionByLatLon(-34.522044, -58.700895, 10);
		
		
		
		dialogo.getContentPane().add(mapa, BorderLayout.CENTER);
		
		
		

	

			mostrarClientes();
			mostrarCamino();


	}
	

	
	// Muestra todos los clientes
	private void mostrarClientes()
	{
		clientes = pedidosGeoLoc.getListaClientes();
		for(Cliente c : clientes )
		{
			
			double latitud = c.getDomicilio().getCoordenada().getLatitud();
			double longitud = c.getDomicilio().getCoordenada().getLongitud();
			
			Coordinate coord = new Coordinate(latitud,longitud);
			MapMarker marker = new MapMarkerDot(c.getNombre(), coord);
			marker.getStyle().setColor(Color.white);
			mapa.addMapMarker(marker);
		}
		
	}
	
	// Muestra un camino (ESTE HAY QUE MODIFICAR)
	private void mostrarCamino()
	{
		Set<Cliente> clientes = pedidosGeoLoc.getListaClientes() ;
		
		ArrayList<Coordinate> lista = new ArrayList<Coordinate>();
			
		for(Cliente c: clientes)
		{
			double latitud = c.getDomicilio().getCoordenada().getLatitud();
			double longitud = c.getDomicilio().getCoordenada().getLongitud();
			lista.add(new Coordinate(latitud, longitud));
		}
		
		
		MapPolygon poligono = new MapPolygonImpl(lista);
		poligono.getStyle().setColor(Color.BLUE);
		mapa.addMapPolygon(poligono);
		
//		MapRectangle rectangulo = new MapRectangleImpl(lista., bottomRight) ;
//		rectangulo.getStyle().setColor(Color.BLUE);
//		mapa.addMapRectangle(rectangulo);
		}


	
	
}

